import 'package:efood_multivendor/controller/location_controller.dart';
import 'package:efood_multivendor/data/model/response/prediction_model.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:flutter/material.dart';
import 'package:flutter_feather_icons/flutter_feather_icons.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class LocationSearchDialog extends StatelessWidget {
  final GoogleMapController mapController;
  final bool isFromCheckout;
  LocationSearchDialog(
      {required this.mapController, this.isFromCheckout = false});

  @override
  Widget build(BuildContext context) {
    final TextEditingController _controller = TextEditingController();

    return GetBuilder<LocationController>(builder: (locationController) {
      return Hero(
        tag: "SearchLocation",
        child: Container(
          padding: EdgeInsets.all(Dimensions.PADDING_SIZE_SMALL),
          alignment: Alignment.topCenter,
          child: Material(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(Dimensions.RADIUS_SMALL)),
            child: SizedBox(
                width: Dimensions.WEB_MAX_WIDTH,
                child: TypeAheadField(
                  textFieldConfiguration: TextFieldConfiguration(
                    controller: _controller,
                    textInputAction: TextInputAction.newline,
                    onEditingComplete: () {
                      Get.back();
                    },
                    autofocus: true,
                    textCapitalization: TextCapitalization.words,
                    keyboardType: TextInputType.text,
                    decoration: InputDecoration(
                      prefixIcon: Icon(
                        FeatherIcons.search,
                        color: Colors.black,
                      ),
                      hintText: 'search_location'.tr,
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(10),
                        borderSide:
                            BorderSide(style: BorderStyle.none, width: 0),
                      ),
                      hintStyle:
                          Theme.of(context).textTheme.headlineMedium?.copyWith(
                                fontSize: Dimensions.fontSizeDefault * 1.4,
                                color: Theme.of(context).disabledColor,
                              ),
                      filled: true,
                      fillColor: Theme.of(context).cardColor,
                    ),
                    style: Theme.of(context).textTheme.headlineMedium?.copyWith(
                          color: Theme.of(context).textTheme.bodyMedium?.color,
                          fontSize: Dimensions.fontSizeLarge,
                        ),
                  ),
                  suggestionsCallback: (pattern) async {
                    return await Get.find<LocationController>()
                        .searchLocation(context, pattern);
                  },
                  getImmediateSuggestions: true,
                  hideOnError: true,
                  hideOnEmpty: true,
                  autoFlipDirection: true,
                  itemBuilder: (context, PredictionModel suggestion) {
                    return Padding(
                      padding: EdgeInsets.all(Dimensions.PADDING_SIZE_SMALL),
                      child: Row(children: [
                        Icon(Icons.location_on),
                        Expanded(
                          child: Text(suggestion.description ?? '',
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: Theme.of(context)
                                  .textTheme
                                  .headlineMedium
                                  ?.copyWith(
                                    color: Theme.of(context)
                                        .textTheme
                                        .bodyMedium
                                        ?.color,
                                    fontSize: Dimensions.fontSizeLarge,
                                  )),
                        ),
                      ]),
                    );
                  },
                  onSuggestionSelected: (PredictionModel suggestion) {
                    Get.find<LocationController>().setLocation(
                        suggestion.placeId!,
                        suggestion.description ?? '',
                        mapController);
                    if (!isFromCheckout) {
                      Get.back();
                    }
                  },
                )),
          ),
        ),
      );
    });
  }
}
